package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-23
 * Time: 19:09
 */
public class ThreadDemon27 {

    public static void main(String[] args) {
        Object locker1 = new Object();
        Thread t1 = new Thread(()->{
            synchronized(locker1){
                try {
                    System.out.println("第一行：t1拿到了锁，现在wait释放锁，陷入阻塞等待");
                    locker1.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("第六行：t2释放锁之后，t1锁住了，打印一下，结束");

            }

        });

        Thread t2 = new Thread(()->{

            try {
                Thread.sleep(1000);
                System.out.println("第二行：确保t1线程能先锁上locker1");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (locker1){
                System.out.println("第三行：t2拿到锁了");
                locker1.notify();
                System.out.println("第四行：t2，notify唤醒t1");
                System.out.println("第五行：t2释放锁");
            }
        });

        t1.start();
        t2.start();
    }
}
